﻿using ScratchNet.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ScratchNet
{
    public class SetAOOutputExpression : Expression
    {
        public override string ReturnType => "any";
        public BoolOperator BoolOperator { get; set; }

        private static List<string> OutPutValues = new List<string>();

        public string OutPutValue { get; set; }

        public SetAOOutputExpression()
        {
            BoolOperator = BoolOperator.TRUE;
        }
        public SetAOOutputExpression(IOBoardContent ioBoardContent)
        {
            BoolOperator = BoolOperator.TRUE;
            if (!OutPutValues.Any() && ioBoardContent.IOTypeCountDict.TryGetValue(LinkIOTypeEmParaDLL.AOut_HD, out var outputs))
            {
                for (int i = 1; i <= outputs.Count; i++)
                {
                    OutPutValues.Add($"output{i}");
                }
            }
            OutPutValue = OutPutValues.FirstOrDefault();
        }
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            return new Completion(null, SetBoolPin());
        }
        public override Descriptor Descriptor
        {
            get
            {
                Descriptor desc = new Descriptor();
                desc.Add(new TextItemDescriptor(this, "EDS2000 设置 AO", true));
                desc.Add(new SelectionItemDescriptor(this, "OutPutValue", OutPutValues, OutPutValues));
                desc.Add(new TextItemDescriptor(this, " 值 "));
                desc.Add(new SelectionItemDescriptor(this, "BoolOperator", new object[] { "TRUE", "FALSE", },
                    new object[] { BoolOperator.TRUE, BoolOperator.FALSE }));
                
                return desc;
            }
        }

        public override string Type
        {
            get { return "SetOutputExpression"; }
        }

        private string SetBoolPin()
        {
            return ($"SetOutputExpression.SetBoolPin({OutPutValues},{BoolOperator});");

        }
    }
}
